package controller;

import entity.MedicineEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import service.MedicineService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@RestController
@RequestMapping("/medicine")
public class MedicineController {

    @Autowired
    private MedicineService medicineService;

    // 根据主键查询
    @GetMapping("/{id}")
    public MedicineEntity getMedicineById(@PathVariable Integer id) {
        return medicineService.getMedicineById(id);
    }

    // 全部查询
    @GetMapping("/list")
    public IPage<MedicineEntity> getAllMedicines(@RequestParam(defaultValue = "1") int page,
                                                 @RequestParam(defaultValue = "10") int size) {
        return medicineService.getAllMedicines(new Page<>(page, size));
    }

    // 模糊查询
    @GetMapping("/search")
    public IPage<MedicineEntity> searchMedicines(@RequestParam String keyword,
                                                 @RequestParam(defaultValue = "1") int page,
                                                 @RequestParam(defaultValue = "10") int size) {
        return medicineService.searchMedicinesFuzzy(keyword, new Page<>(page, size));
    }

    // 增加药品
    @PostMapping
    public boolean addMedicine(@RequestBody MedicineEntity medicine) {
        return medicineService.addMedicine(medicine);
    }

    // 删除药品
    @DeleteMapping("/{id}")
    public boolean deleteMedicine(@PathVariable Integer id) {
        return medicineService.deleteMedicine(id);
    }

    // 修改药品（增量修改）
    @PatchMapping("/{id}")
    public boolean updateMedicine(@PathVariable Integer id, @RequestBody MedicineEntity medicine) {
        medicine.setId(id);
        return medicineService.updateMedicine(medicine);
    }
}